/*
 * @Author: Zhang Kai
 * @Date: 2021-11-15 16:33:00
 * @LastEditors: mayijun
 * @LastEditTime: 2022-08-11 16:46:38
 * @FilePath: /metricsx-standard-ui/vue.config.js
 */
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const proxy = require('./proxy');
const { defineConfig } = require('@vue/cli-service');
const { ArcoResolver } = require('unplugin-vue-components/resolvers');
const webpack = require('webpack');

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = defineConfig({
  pages: {
    index: {
      entry: './src/index.ts'
    }
  },
  publicPath: process.env.VUE_APP_PUBLICPATH,
  lintOnSave: false,
  devServer: {
    disableHostCheck: true,
    port: 5001,
    host: '0.0.0.0',
    headers: {
      'Access-Control-Allow-Origin': '*'
    },
    proxy,
    hot: false
  },
  chainWebpack: (config) => {
    config.module
      .rule('vue')
      .use('vue-loader')
      .options({
        hotReload: false
      })
      .tap((options) => {
        return {
          ...options,
          refSugar: true
        };
      });
    //打包拆分
    config.optimization.minimize(true);
    config.optimization.splitChunks({
      chunks: "all",
      maxInitialRequests: Infinity,
      minSize: 200000, // 依赖包超过300000bit将被单独打包
      automaticNameDelimiter: "-",
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name(module) {
            const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
            return `chunk.${packageName.replace("@", "")}`;
          },
          priority: 10
        }
      }
    });
  },
  configureWebpack: {
    devtool: process.env.VUE_APP_PUBLISHNAME === 'PRO' ? false : 'source-map',
    resolve: {
      alias: {
        '@': resolve('src'),
        utils: resolve('src/utils'),
        src: resolve('src'),
        ibs: resolve('src/views/IBS'),
        vue$: `${path.resolve(__dirname, './node_modules/vue')}`,
        '@arco-design/web-vue': `${path.resolve(
          __dirname,
          './node_modules/@arco-design/web-vue'
        )}`
      }
    },
    plugins: [
      new webpack.NormalModuleReplacementPlugin(
        /(.*)\/resize-observer-v2.js$/,
        `${path.resolve(__dirname, './lib/resize-observer-v2.js')}`
      ),
      new webpack.container.ModuleFederationPlugin({
        name: 'app_general',
        filename: 'remoteEntry.js',
        remotes: {
          // app_exposes: "app_exposes@http://localhost:8082/remoteEntry.js",
          metadatax: `metadatax@${process.env.VUE_APP_METADATAX_FORNT}/metadatax/remoteEntry.js`,
          dio: `dio@${process.env.VUE_APP_DIO_FORNT}/dio/remoteEntry.js`
        },
        shared: {
          vue: {
            singleton: true
          },
          'vue-router': {
            singleton: true
          },
          pinia: {
            singleton: true
          }
        }
      }),
      require('unplugin-vue-components/webpack')({
        dts: true,
        extensions: ['vue', 'tsx'],
        resolvers: [
          ArcoResolver({
            importStyle: false
          })
        ]
      })
    ],
    optimization: {
      splitChunks: {
        chunks: 'all'
      }
    }
  }

  // transpileDependencies: true,
});
